<template>
  <div>
    <div class="kuangja">
      <h3>{{tableData.title}}</h3>
      <hr>
      <p>{{tableData.content}}</p>
      <p>附件</p>
    </div>
  
    <el-button size="small" type="success" @click="mod(tableData.id)">编辑</el-button>

      <el-dialog
              title="提示"
              :visible.sync="centerDialogVisible"
              width="80%"
              center>
<!--           //增加表单-->
          <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">

                <el-form-item label="活动名" prop="title">
                      <el-input  v-model="form.title"></el-input>
              </el-form-item>

                <el-form-item label="描述" prop="content">
                    <WangEditor @sendEditor="sendEditor" ref="myEditor"></WangEditor>
                </el-form-item>
                <el-form-item label="附件名" prop="name">
                      <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="文件" prop="filePath">
                    <!-- 上传文件 -->
                 <el-upload
                     class="upload-demo"
                     action="http://localhost:9001/jk/notice/admin/uploadeditor"
                    :data={id:id}
                    :on-change="handleChange"
                    :limit="1"
                    :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <span style="margin-left:20px">{{form.filePath}}</span>
            </el-upload>
                </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取 消</el-button>
           <el-button type="primary" @click="save">确 定</el-button>
            </span>
      </el-dialog>
  </div>
</template>

<script>
import WangEditor from '@/views/editor/wangEditor.vue'
    export default {
        components:{
            WangEditor
        },
        name: "News",
        data() {
         
            return {
                tableData: [],
                fileList:[],
                total:0,
                 name:'',
                type:6,
                centerDialogVisible:false,
                id:'',
                form:{
                    title:'',
                    name:'',
                    content:'',
                    filePath:'',
                    type:6

                },
                rules: {
                   
                    name: [
                        { required: true, message: '请输入名字', trigger: 'blur' },
                    ],
                   

                }

            }

        },
        methods:{
            sendEditor(val){
                this.form.content = val
             
            },
            resetForm() {
                this.$refs.form.resetFields();
            },
            mod(id){
              
              this.$axios.get(this.$httpUrl+'/jk/notice/admin/edit?id='+id).then(res=>res.data).then(res=>{
                  if (res.resCode=="000000"){
              this.centerDialogVisible=true
              this.$nextTick(()=>{
                  this.id=res.data.id
                  this.form.id = res.data.id
                  this.form.title = res.data.title
                  this.form.name = res.data.title
                  this.form.filePath = res.data.filePath
                  this.$refs.myEditor.editor.txt.html(res.data.content)
                  this.form.content =   this.$refs.myEditor.editor.txt.html('<p>' +  res.data.content+ '</p>')
              })    
                  }else{
                  this.$message({
                  message: '操作失败',
                  type: 'error'
              });
                      }
                  })
          },
           
            // 修改
            doMod(){
            
                this.$axios.put(this.$httpUrl+'/user/unit/update',
                    this.form

                ).then(res=>res.data).then(res=>{
                    
                    if (res.resCode=='000000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面

                        this.centerDialogVisible=false
                        this.loadPost()
                        // this.resetForm()

                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })
            },

            save(){
                this.$refs.form.validate((valid) => {
                    if (valid) {
                      
                            this.doMod()
                       

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            handleChange(file, fileList) {
                this.form.filePath=file.response
                var a=""+this.form.filePath
                this.form.id = a.slice(9,28)
                this.fileList = fileList.slice(-3);
            },
            loadPost(){
             
             this.$axios.get(this.$httpUrl+'/jk/notice/admin/brief/1').then(res=>res.data).then(res=>{
                 if (res.resCode=='000000'){
                     this.tableData=res.data
                     this.tableData.content = res.data.content.replace(/<[^>]+>/g,"")
                     
                     this.total=res.data.total

                 }else{
                     alert('获取数据失败')
                 }

             })
         }
        },
        beforeMount() {
            
            this.loadPost();

        }
    }
</script>

<style lang="less" scoped>.kuangja{
    margin-left: 160px;
  width: 1200px;
  min-height: 50px;
  height: auto;
  background-color: aliceblue;
  border: 1 px solid black;
}

</style>